package cn.neusoft.service.impl;

import cn.neusoft.domain.*;
import cn.neusoft.enums.OrderStatus;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.feignClients.CommodityFeignClient;
import cn.neusoft.mapper.OrderMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.service.OrderService;
import cn.neusoft.util.EnumUtils;
import cn.neusoft.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.math.BigDecimal;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {
  @Autowired
  OrderMapper orderMapper;

  @Autowired
  CommodityFeignClient commodityFeignClient;

  //查询库存量
  @Override
  public Long findOccupiedAmount(String commodity_id) {
    return orderMapper.findOccupiedAmount(commodity_id);
  }

  //查询实时库存量
  @Override
  public Long findTrueAmount(String commodity_id) {
    return orderMapper.findTrueAmount(commodity_id);
  }

  //查询正常订单订货数
  @Override
  public Long findNormalOrderAmount(String commodity_id) {
    return orderMapper.findNormalOrderAmount(commodity_id);
  }

  //查询换货订单订货数
  @Override
  public Long findExchangeOrderAmount(String commodity_id) {
    return orderMapper.findExchangeOrderAmount(commodity_id);
  }

  //修改订单状态
  @Transactional
  @Override
  public void judgeAmount(Order order) {
    orderMapper.judgeAmount(order);
  }

  //修改虚拟库存
  @Transactional
  @Override
  public void updateoccupamount(String commodity_id,Long occupied_amount) {
    orderMapper.updateoccupamount(commodity_id,occupied_amount);
  }

  //新增父类订单
  @Transactional
  @Override
  public void addOrder(Order order) {
    // 调用配送模块的接口，得到当前商品的原价和折扣，计算出当前商品的单价
    ResultBean resultBean = commodityFeignClient.getById(order.getOrder_commodity_id());

    System.out.println(resultBean);

    Integer code = resultBean.getCode();
    String msg = resultBean.getMsg();
    Commodity commodity = (Commodity) resultBean.getData();

    // 异常
    if(!Objects.equals(code, 200)){
      if(Objects.equals(code, 500)){  // 非AppException
        throw new RuntimeException();
      }else{  // AppException
        throw new AppException(code, msg);
      }
    }else if(commodity == null){ // 没有对应id的商品
      throw new AppException(AppExceptionCodeMsg.COMMODITY_NOT_EXISTS);
    }

    // 当前商品的单价 = 原价*折扣
    BigDecimal price = BigDecimal.valueOf(commodity.getOriginal_price() * commodity.getDiscount());

    order.setPrice(price);

    orderMapper.addOrder(order);
  }

  //新增子类型正常订单
  @Transactional
  @Override
  public void addOrder_normal(Order_normal order_normal) {
    orderMapper.addOrder_normal(order_normal);
  }

  //新增子类型换货订单
  @Transactional
  @Override
  public void addOrder_exchange(Order_exchange order_exchange) {
    orderMapper.addOrder_exchange(order_exchange);
  }

  //新增子类型退货订单
  @Transactional
  @Override
  public void addOrder_return(Order_return order_return) {
    orderMapper.addOrder_return(order_return);
  }

  //查询客户下属订单
  @Transactional
  @Override
  public PageBean<Order> findCustoOrderByPage(BaseQuery query) {
    //构建结果对象
    PageBean<Order> pageBean = new PageBean<>();
    //开启分页
    PageHelper.startPage(query.getPageNow(),query.getPageSize());
    //分页查询得到当页的集合
    List<Order> orderList = orderMapper.findCustoOrderByPage(query);
    //创建一个分页的实体PageInfo
    PageInfo<Order> pageInfo = new PageInfo<>(orderList);
    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setRowData(pageInfo.getList());
    return pageBean;
  }

  //查询正常订单
  @Transactional
  @Override
  public NormalOrderBean findNormalOrder(Order order){
    return orderMapper.findNormalOrder(order);
  }

  //查询换货订单
  @Transactional
  @Override
  public ExchangeOrderBean findExchangeOrder(Order order){
    return orderMapper.findExchangeOrder(order);
  }

  //查询正常订单
  @Transactional
  @Override
  public ReturnOrderBean findReturnOrder(Order order){
    return orderMapper.findReturnOrder(order);
  }

  @Transactional
  @Override
  public PageBean<NormalOrderBean> findNormalOrderByPage(BaseQuery query) {
    // 构建结果对象
    PageBean<NormalOrderBean> pageBean = new PageBean<>();

    // 开启分页
    PageHelper.startPage(query.getPageNow(), query.getPageSize());

    // 查询得到当前页的集合
    List<NormalOrderBean> normalOrderList = orderMapper.findOrderNormalByCondition(query);

    // 创建一个分页的实体PageInfo
    PageInfo<NormalOrderBean> pageInfo = new PageInfo<>(normalOrderList);

    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setRowData(pageInfo.getList());

    return pageBean;
  }

  @Transactional
  @Override
  public PageBean<ExchangeOrderBean> findExchangeOrderByPage(BaseQuery query) {
    // 构建结果对象
    PageBean<ExchangeOrderBean> pageBean = new PageBean<>();

    // 开启分页
    PageHelper.startPage(query.getPageNow(), query.getPageSize());

    // 查询得到当前页的集合
    List<ExchangeOrderBean> exchangeOrderList = orderMapper.findOrderExchangeByCondition(query);

    // 创建一个分页的实体PageInfo
    PageInfo<ExchangeOrderBean> pageInfo = new PageInfo<>(exchangeOrderList);

    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setRowData(pageInfo.getList());

    return pageBean;
  }

  @Transactional
  @Override
  public PageBean<ReturnOrderBean> findReturnOrderByPage(BaseQuery query) {
    // 构建结果对象
    PageBean<ReturnOrderBean> pageBean = new PageBean<>();

    // 开启分页
    PageHelper.startPage(query.getPageNow(), query.getPageSize());

    // 查询得到当前页的集合
    List<ReturnOrderBean> returnOrderList = orderMapper.findOrderReturnByCondition(query);

    // 创建一个分页的实体PageInfo
    PageInfo<ReturnOrderBean> pageInfo = new PageInfo<>(returnOrderList);

    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setRowData(pageInfo.getList());

    return pageBean;
  }

  @Transactional
  @Override
  public Order1 findOrderNormalByCondition(BaseQuery query) {
    return orderMapper.findOrderNormalByCondition1(query);
  }

  @Transactional
  @Override
  public Order1 findOrderExchangeByCondition(BaseQuery query) {
    return orderMapper.findOrderExchangeByCondition1(query);
  }

  @Transactional
  @Override
  public Order1 findOrderReturnByCondition(BaseQuery query) {
    return orderMapper.findOrderReturnByCondition1(query);
  }

  @Override
  public PageBean<Order1> findOrderByPage(BaseQuery query) {
    final List<Integer> order_kinds = Arrays.asList(1,2,3);

    OrderQuery orderQuery = (OrderQuery) query;

    Integer order_kind = orderQuery.getOrder_kind();

    // 订单类型非法
    if(!order_kinds.contains(order_kind)){
      throw new AppException(AppExceptionCodeMsg.ORDER_KIND_INVALID);
    }

    List<Order1> orderList = new ArrayList<>();

    // 构建结果对象
    PageBean<Order1> pageBean = new PageBean<>();

    // 开启分页
    PageHelper.startPage(query.getPageNow(), query.getPageSize());

    // 查正常订单
    if(Objects.equals(order_kind, 1)){
      orderList = orderMapper.findOrderNormalListByCondition(query);
    }

    // 查退货单
    if(Objects.equals(order_kind, 2)){
      orderList = orderMapper.findOrderReturnListByCondition(query);
    }

    // 查换货单
    if(Objects.equals(order_kind, 3)){
      orderList = orderMapper.findOrderExchangeListByCondition(query);
    }

    // 创建一个分页的实体PageInfo
    PageInfo<Order1> pageInfo = new PageInfo<>(orderList);

    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setRowData(pageInfo.getList());

    return pageBean;
  }

  @Override
  public void updateOrderStatus(Order1 order) {
    // 订单状态非法
    if(!EnumUtils.isInclude(OrderStatus.class, order.getOrder_status())){
      throw new AppException(AppExceptionCodeMsg.ORDER_STATUS_INVALID);
    }

    orderMapper.updateOrderStatus(order);
  }

  @Override
  public void updateOrderRestock(Order order){
    orderMapper.updateOrderRestock(order);
  }
  @Override
  public List<Order> findAll(){
    List<Order> orders = orderMapper.findAll();
    return orders;
  }
  @Override
  public void updateOrderStatusWhenCreate(Order order) {
    orderMapper.updateOrderStatusWhenCreate(order);
  }

  @Override
  public int countMatchingOrders(String commodity_id){
    int count = orderMapper.countMatchingOrders(commodity_id);
    return count;
  };
}
